package com.chinastock.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangzhendong
 * 请求，响应 日志
 */
@Data
public class AppMessage extends Message {
    private String type;
    private String url;
    private Map<String, String> headers;
    private Object obj;
    private String contentType;
    private String excludes;

    @Override
    public String toString() {
        StringBuilder content = new StringBuilder();
        content.append("[type=" + type + ",");
        if (StringUtils.isNotBlank(url)) {
            content.append("url=" + url + ",");
        }
        if (Objects.nonNull(headers) && headers.size() > 0) {
            content.append("headers=[");
            for (Map.Entry<String, String> entry :
                    headers.entrySet()) {
                content.append(entry.getKey() + ":" + entry.getValue() + "|");
            }
            content.append("],");
        }
        if (Objects.nonNull(obj)) {
            content.append("content=");
            if (StringUtils.equals(contentType.toLowerCase(), ContentType.JSON.getType().toLowerCase())) {
                if (obj instanceof String) {
                    if (StringUtils.isNotBlank(excludes)) {
                        String[] excludeArray = StringUtils.split(excludes, ",");
                        SimplePropertyPreFilter excludeFilter = new SimplePropertyPreFilter();
                        for (String exclude : excludeArray) {
                            excludeFilter.getExcludes().add(exclude);
                        }
                        Object jsonObject = JSONObject.parse((String) obj);
                        content.append(JSON.toJSONString(jsonObject, excludeFilter, SerializerFeature.WriteNullStringAsEmpty));
                    } else {
                        content.append(obj);
                    }
                } else {
                    if (StringUtils.isNotBlank(excludes)) {
                        String[] excludeArray = StringUtils.split(excludes, ",");
                        SimplePropertyPreFilter excludeFilter = new SimplePropertyPreFilter();
                        for (String exclude : excludeArray) {
                            excludeFilter.getExcludes().add(exclude);
                        }
                        content.append(JSON.toJSONString(obj, excludeFilter, SerializerFeature.WriteNullStringAsEmpty));
                    } else {
                        content.append(JSON.toJSONString(obj));
                    }
                }
            } else if (StringUtils.equals(contentType.toLowerCase(), ContentType.TEXT.getType().toLowerCase())) {
                content.append(obj);
            } else if (ContentType.PART.getType().toLowerCase().equals(contentType.toLowerCase())) {
                content.append("[");
                if (Objects.nonNull(obj)) {
                    List<FilePart> parts = (List<FilePart>) obj;
                    for (FilePart part : parts
                    ) {
                        content.append(part.toString());
                    }
                }
                content.append("]");
            }
        }
        return content.toString();
    }
}
